import FPJDefine from "../Game/FPJ/Prefabs/FPJDefine";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BtnBackLiuJi extends cc.Component {

    @property(cc.Node)
    BtnLiuJi: cc.Node = null;


    gameChange() {
        //如果有留机数据才显示按钮
        if (this.BtnLiuJi) {
            this.BtnLiuJi.active = FPJDefine.getHoldTable_id() == -1 ? false : true;
        }
    }

    onEnable() {
        cc.vv.eventCustom.on("GameChange", this.gameChange.bind(this));
        //如果有留机数据才显示按钮
        this.BtnLiuJi.active = FPJDefine.getHoldTable_id() == -1 ? false : true;
    }
    onDisable() {
        cc.vv.eventCustom.off("GameChange", this.gameChange);
    }
    //返回留机器
    onClickBackLiuJi() {

        let table_id = FPJDefine.getHoldTable_id();
        if (table_id == -1) { return };

        //
        let gameId = cc.vv.GLDefine.EGameID.FPJ;
        cc.vv.GLUtils.sendRequest("ATTEnterTable", {
            table_id: FPJDefine.getHoldTable_id(),
        }, (error, data) => {
            if (!error) {
                if (data.errorcode) {
                    cc.vv.GLUtils.showTips(data.errordes);
                } else {
                    FPJDefine.setTableDetail(data.table_data);
                    FPJDefine.setcurrentTableId(data.table_data.table_id)
                    FPJDefine.setCurrentState(data.table_data.status)
                    let _path = cc.vv.GLDefine.getEnglishNameById(gameId);
                    cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path);
                    cc.vv.sceneMgr.openScene(_path, data, () => {
                        cc.vv.eventCustom.emit("openGame");
                        cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
                    });
                }
            }
            // else {
            //     cc.vv.GLUtils.showTips(data.errordes);
            // }
        });
    }
}
